from datetime import datetime
from sqlalchemy import Column, Integer, String
from exception import JsonException
from model import db

# 定义映射类User，其继承上一步创建的Base
class Config(db.Base):
    # 指定本类映射到users表
    __tablename__ = 'config'

    id = Column(Integer, primary_key=True, autoincrement=True)
    gzh_appid = Column(String)
    gzh_secret = Column(String)
    day_commit_limit = Column(Integer) # 每天交卷次数限制
    point_k = Column(Integer) # 每道题的分值

    start_time = Column(String)
    end_time = Column(String)

    def to_json(self):
        dict = self.__dict__
        if "_sa_instance_state" in dict:
            del dict["_sa_instance_state"]
        return dict


# 获取配置文件
def get_config():
    one = None
    with db.Session() as session:
        one = session.query(Config).first()
        if one == None:
            raise JsonException(f"非法请求：不存在配置信息")
    return one